"""
@Author  : 康帅
@Time    : 2021/3/22 13:23
@Function: 迭代模式基类
"""


class BaseIterator:
    """ 迭代器 """

    def __init__(self, data):
        self.__data = data

    def to_begin(self):
        """ 将指针移至开始位置 """

        self.__cur_idx = -1

    def to_end(self):
        """ 将指针移至结尾位置 """

        self.__cur_idx = len(self.__data)

    def next(self):
        """ 移动至下一个元素 """

        if (self.__cur_idx < len(self.__data) - 1):
            self.__cur_idx += 1
            return True
        else:
            return False

    def previous(self):
        """ 移至上一个元素 """

        if self.__cur_idx > 0:
            self.__cur_idx -= 1
            return True
        else:
            return False

    def current(self):
        """ 获取当前的元素 """

        return self.__data[self.__cur_idx] if self.__cur_idx < len(self.__data) and self.__cur_idx >= 0 else None


if __name__ == '__main__':
    print("从前往后遍历：")
    iterator = BaseIterator(range(0, 10))
    iterator.to_begin()
    while (iterator.next()):
        customer = iterator.current()
        print(customer, end="\t")
    print()

    print("从后往前遍历")
    iterator.to_end()
    while (iterator.previous()):
        customer = iterator.current()
        print(customer, end="\t")
